package com.xing.book.dao.impl;

import com.xing.book.dao.UserDao;
import com.xing.book.domin.User;
import com.xing.book.util.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public User select(String username, String password) {
        /*Connection connection = DbUtils.getConnection();
        PreparedStatement pstat=null;
        ResultSet rs=null;

        try {
            pstat = connection.prepareStatement("select * from user where username=? and password=?");
            pstat.setString(1,username);
            pstat.setString(2,password);
            rs = pstat.executeQuery();

            User  user=null;
            if (rs.next()){
                int id=rs.getInt("id");
                String realname=rs.getString("realname");
                String email=rs.getString("email");
                String gender=rs.getString("gender");
                int flag=rs.getInt("flag");
                int role=rs.getInt("role");
                user=new User(id,username,password,realname,email,gender,flag,role);
            }
            return user;


        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DbUtils.closeAll(rs,pstat,connection);
        }*/
        String sql="select * from user where username=? and password=?";
        List<User> users = DbUtils.selectList(sql, User.class, username, password);
        if(users!=null&&users.size()>0){
            return users.get(0);
        }
        return null;

    }
}
